package com.wnhz.work.yz.activity.register;

import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * ---注册2
 */
public class Register2Activity extends BaseActivity implements View.OnClickListener {
    private TextView tv_right;
    private String phone;
    private String code;

    private TextView tv_phone;
    private EditText et_code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register2);

        initData();
        initView();
    }

    private void initData() {
        phone = getIntent().getStringExtra("phone");
    }

    private void initView() {
        findViewById(R.id.view_main_middle_title).setVisibility(View.GONE);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.btn_main_right).setVisibility(View.VISIBLE);
        findViewById(R.id.btn_main_right).setOnClickListener(this);
        tv_right = (TextView) findViewById(R.id.view_main_right_text);
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setText("下一步");
        tv_right.setTextColor(0xff008c99);

        tv_phone = (TextView) findViewById(R.id.tv_phone);
        et_code = (EditText) findViewById(R.id.et_code);

        tv_phone.setText(setSpannableString("我们向 ", phone, " 发送了一条短信，请将其中的验证码输入。"));


    }

    private static final String TAG = "Register2Activity";



    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //下一步
            case R.id.btn_main_right:
                code = et_code.getText().toString();
                if (!"".equals(code)) {
                    upverify();
                } else {
                    MyToast("验证码不能为空");
                }
                break;

            default:
                break;
        }
    }

    private void upverify() {
        showDialog();
        code = et_code.getText().toString();
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);
        map.put("phone", phone);
        XUtil.Post(Url.USER_CHECKCODE, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                closeDialog();
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: inputCode= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        MyToast(info);
                        Intent intent = new Intent(Register2Activity.this, Register3Activity.class);
                        intent.putExtra("phone", phone);
                        intent.putExtra("code", code);
                        startActivity(intent);
                    } else {
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });

    }

    /**
     * 更改字体
     *
     * @return
     */
    public SpannableString setSpannableString(String s1, String s2, String s3) {
        StringBuilder builder = new StringBuilder();
        builder.append(s1);
        builder.append(s2);
        builder.append(s3);
        SpannableString member = new SpannableString(builder);
//        member.setSpan(new RelativeSizeSpan(0.8f), 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//        member.setSpan(new RelativeSizeSpan(0.8f), builder.length() - s2.length(), builder.length(),
//                Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        member.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.main_color)), s1.length(), builder.length() - s3.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return member;
    }
}
